import unittest
from datetime import datetime

from django.test import TestCase
from backend.po.supplier import Supplier
from backend.po.buy_order import BuyOrder
from backend.services import SupplierService
from backend.services import BuyOrderService


class BuyOrderSvcTestCast(TestCase):
    def setUp(self):
        self.order_svc = BuyOrderService()
        self.supplier_svc = SupplierService()

    def test_add_with_default_supplier(self):
        order_dft_sup = BuyOrder(buy_date_time=datetime.now(),
                                 digest='buy lemon',
                                 amount=1,
                                 price=68.88,
                                 unit='箱')
        self.assertEqual(0, order_dft_sup.id)
        order_dft_sup = self.order_svc.create(order_dft_sup)
        dft_sup_id = order_dft_sup.supplier.id
        self.assertNotEqual(0, order_dft_sup.id)
        self.assertNotEqual(None, order_dft_sup.supplier)

        supplier = Supplier(supplier_name='JD',
                            contact_name='LiuQiangdong',
                            contact_no='12812348765')
        supplier = self.supplier_svc.create(supplier)

        order_dft_sup.supplier = supplier
        self.order_svc.update(order_dft_sup)

        order_up_sup = self.order_svc.retrieve(order_dft_sup.id)
        self.assertNotEqual(dft_sup_id, order_up_sup.supplier.id)
        self.assertEqual(supplier.contact_name, order_up_sup.supplier.contact_name)

    def test_crud(self):
        supplier = Supplier(supplier_name='JD',
                            contact_name='LiuQiangdong',
                            contact_no='12812348765')
        self.assertEqual(0, supplier.id)

        order = self.order_svc.create(BuyOrder(supplier=supplier,
                                               buy_date_time='2024-04-01 14:57:23',
                                               digest='buy apples',
                                               amount=1,
                                               price=168.88,
                                               unit='箱'))
        self.assertEqual('buy apples', order.digest)
        self.assertNotEqual(0, order.supplier.id)
        self.assertEqual(supplier.contact_name, order.supplier.contact_name)

        orders = self.order_svc.list()
        self.assertEqual(1, len(orders))
        self.assertEqual('buy apples', orders[0].digest)
        self.assertEqual(supplier.contact_name, orders[0].supplier.contact_name)

        order2 = self.order_svc.create(BuyOrder(supplier=supplier,
                                                buy_date_time='2024-04-02 14:57:23',
                                                digest='buy computer',
                                                amount=1,
                                                price=4688.88,
                                                unit='台'))
        self.assertEqual('buy computer', order2.digest)
        self.assertEqual(supplier.contact_name, order2.supplier.contact_name)

        orders = self.order_svc.list()
        self.assertEqual(2, len(orders))
        self.assertEqual('buy computer', orders[1].digest)
        self.assertEqual(supplier.contact_name, orders[1].supplier.contact_name)

        order_qry = self.order_svc.retrieve(order2.id)
        self.assertEqual('buy computer', order_qry.digest)
        self.assertFalse(order_qry.is_payed)
        self.assertFalse(order_qry.is_take_delivery)

        # deliveries = svc.delivery_list(order2.id)
        # self.assertEqual(0, len(deliveries))

        # delivery_svc = DeliveryService()
        # deli = TakeDelivery(buy_order=order2,
        #                     take_delivery_date_time='2024-04-03 13:41:00',
        #                     take_delivery_no='132345678')
        # delivery_svc.create(deli)
        # self.assertTrue(order2.is_take_delivery)

        # deliveries = svc.delivery_list(order2.id)
        # self.assertEqual(1, len(deliveries))
        # self.assertEqual('132345678', deliveries[0].take_delivery_no)


if __name__ == '__main__':
    unittest.main(verbosity=2)
